//
//  LBZuijiaCell.m
//  V-Learn
//
//  Created by Apple on 2018/12/27.
//  Copyright © 2018 yxk. All rights reserved.
//

#import "LBZuijiaCell.h"
#import "WKHotmenModel.h"
#import "WKHotclockInfoVOListModel.h"
@implementation LBZuijiaCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        [self setupUI];
    }
    return self;
}
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *identifferCell = @"LBZuijiaCell";
    LBZuijiaCell *cell = [tableView dequeueReusableCellWithIdentifier:identifferCell];
    if (!cell) {
        cell = [[LBZuijiaCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifferCell];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    return cell;
}
- (void)setupUI{

    UIScrollView *scorllView = [[UIScrollView alloc] init];
    self.scorllView = scorllView;
    scorllView.userInteractionEnabled = YES;
    scorllView.frame = CGRectMake(15, 0, ScreenW, 133);
    //    [self.contentView addSubview:scorllView];
    
    scorllView.showsVerticalScrollIndicator = NO;
    scorllView.showsHorizontalScrollIndicator = NO;
    CGFloat btnW = (SCREEN_WIDTH-3*15)/2;
    for (int i=0; i<self.dataArray.count; i++) {
        WKHotmenModel *model2 = self.dataArray[i];
        WKHotclockInfoVOListModel *model = model2.clockInfoVOList[0];
        UIView *smallView = [[UIView alloc] init];
        smallView.userInteractionEnabled = YES;
        
        smallView.layer.cornerRadius = 8;
        smallView.layer.masksToBounds = YES;
//        UIButton *courseButton = [[UIButton alloc] init];
//        courseButton.tag = 109+i;
//        courseButton.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
//        self.optimumButton = courseButton;
        
        UIImageView *icon = [[UIImageView alloc] init];
        icon.tag = 109+i;
        icon.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
        self.icon = icon;
        smallView.backgroundColor = [UIColor clearColor];
        
        smallView.frame = CGRectMake((btnW+15)*i, 0, btnW, 100);
    
        NSString *url = [NSString stringWithFormat:@"%@%@",Public_URL,model.videoCover];

        icon.userInteractionEnabled = YES;
        icon.layer.cornerRadius = 8;
        icon.layer.masksToBounds = YES;

        __weak typeof(self)weakSelf = self;
                [icon sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"wk_list_bgimg"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {

        }];
        /**
         UIViewContentModeScaleToFill,
         UIViewContentModeScaleAspectFit,    
         UIViewContentModeScaleAspectFill,
         */
        //处理图片左右侧空白
        icon.contentMode = UIViewContentModeScaleAspectFill;
        icon.image = [self compressImageWith:icon.image width:btnW height:100];

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOpimumBtn:)];
        [icon addGestureRecognizer:tap];
        
        tap.view.tag = 109+i;
        [smallView addSubview:icon];
        [icon makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.bottom.mas_equalTo(0);
        }];

        //播放按钮
        self.leftPlayIcon = [[UIImageView alloc] init];
        self.leftPlayIcon.layer.cornerRadius = 10;
        self.leftPlayIcon.layer.masksToBounds = YES;
        self.leftPlayIcon.image = LBImage(@"play");
        [icon addSubview:self.leftPlayIcon];
        
        [self.leftPlayIcon makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(10);
            make.bottom.mas_equalTo(-10);
            make.size.mas_equalTo(CGSizeMake(20, 20));
        }];

        [scorllView addSubview:smallView];
        
        UILabel *label = [UILabel labelWithText:@"" andTextColor:rgb(51,51,51) andName:nil andFontSize:14];
        label.text = model2.clockTitle;
        [scorllView addSubview:label];
        [label makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(smallView.mas_bottom).mas_equalTo(10);
            make.left.mas_equalTo(smallView.mas_left);
            make.right.mas_equalTo(smallView.mas_right);
        }];
    }
    scorllView.contentSize = CGSizeMake((btnW*self.dataArray.count)+(15*(self.dataArray.count+1)), 133);//
    [self.contentView addSubview:scorllView];

}
- (UIImage *)compressImageWith:(UIImage *)image width:(float)width height:(float)height
{
    float imageWidth = image.size.width;
    float imageHeight = image.size.height;
    
    float widthScale = imageWidth /width;
    float heightScale = imageHeight /height;
    
    // 创建一个位图上下文，并把它设置为当前正在使用的上下文
    UIGraphicsBeginImageContext(CGSizeMake(width, height));
    
    if (widthScale > heightScale) {//设置压缩比例
        [image drawInRect:CGRectMake(0, 0, imageWidth /heightScale , height)];
    }
    else {
        [image drawInRect:CGRectMake(0, 0, width , imageHeight /widthScale)];
    }
    
    // 从当前context中创建一个改变大小后的图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    
    return newImage;
    
}
//img:为传入的图片;   size:为放置图片区域的大小;  scaledImage:为返回压缩后的图片
+ (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context
    UIGraphicsBeginImageContext(size);
    // 绘制改变大小的图片
    [img drawInRect:CGRectMake(0,0, size.width, size.height)];
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage =UIGraphicsGetImageFromCurrentImageContext();
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    //返回新的改变大小后的图片
    return scaledImage;
}
- (void)tapOpimumBtn:(UITapGestureRecognizer *)sender{
    NSInteger index = sender.view.tag-109;
    if ([self.delegate respondsToSelector:@selector(clickLBZuijiaCellButton:WithArray:)]) {
        [self.delegate clickLBZuijiaCellButton:index WithArray:self.dataArray];
    }
}
- (void)setDataArray:(NSMutableArray *)dataArray{
    _dataArray = dataArray;
    
    [self.scorllView removeFromSuperview];
    [self setupUI];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
